#define ALLEGRO_USE_CONSOLE

#include <allegro.h> 
#include <iostream>
#include <fstream>
using namespace std;

char color(int index){
	char c;
	switch(index){
		case 249: c='r'; break;
		case 252: c='b'; break;
		case 250: c='g'; break;
		case 255: c='w'; break;
		case 0: c='n'; break;
		default: c='e'; //error index

	}
	return c;
}

int main(int argc, char *argv[]){

	int datacount = 0;

	allegro_init();
	
	BITMAP *my_pic = NULL;
	my_pic = load_bitmap("test.bmp", NULL);

	int width = my_pic->w;
	int height = my_pic->h;

	ofstream f;
    f.open("log.txt", ofstream::out);

	f << height << endl;

	int count = 0;
	int last_color = -1;

	for(int x = (width-1); x >= 0; x--){
		for(int y = (height-1); y >= 0; y--){

				int pixel = getpixel(my_pic,x,y);
				
				if(last_color == -1)
					last_color = pixel;

				switch(pixel){
					//RED
					case 249:	if(last_color == 249){
									count++;
								}else{
									datacount++;
									f << count << color(last_color);
									count = 1;
									last_color = 249;
								};
								break;
					//GREEN
					case 250:	if(last_color == 250){
									count++;
								}else{
									datacount++;
									f << count << color(last_color);
									count = 1;
									last_color = 250;
								};
								break;
					//BLUE
					case 252:   if(last_color == 252){
									count++;
								}else{
									datacount++;
									f << count << color(last_color);
									count = 1;
									last_color = 252;
								};
								break;
					//WHITE
					case 255:	if(last_color == 255){
									count++;
								}else{
									datacount++;
									f << count << color(last_color);
									count = 1;
									last_color = 255;
								};
								break;
					//BLACK
					case 0:		if(last_color == 0){
									count++;
								}else{
									datacount++;
									f << count << color(last_color);
									count = 1;
									last_color = 0;
								};
								break;
					//ERROR COLOR
					default: f << -1; break; 
				}
		}
		//last line
		datacount++;
		f << count << color(last_color);
		f << endl;
		count = 0;
		last_color = -1;
	}

	//Save count of data in file
	f << datacount << endl;

	f.close();
	destroy_bitmap(my_pic);
	return 0; 
}


